const MYSQL = require("../../sql/mysql");
const {formatMenusList} = require("../../utils/format")
// MYSQL数据库配置信息
const MYSQLConfig = require("../../sql/database");
// 角色菜单表
let roleMenuSqlName = "role_menu";
// 菜单表
let menuSqlName = "menus"

const existRole = require("./exist")

/**
 * 获取角色所拥有的菜单列表
 * @param {object} params
 * @param {string} params.id 角色id
 * @returns {Promise<{code:string,msg:string}>}
 */
module.exports = async (params = {}) => {
  try {
    let { id } = params;
    if (!id) {
      throw { code: 500, msg: "id不能为空" };
    }

    // 是否采用Redis服务获取数据
    let ifRedisSelect = true 

    



    // await existRole({id})

    let query = `
      SELECT ${menuSqlName}.* FROM ${menuSqlName} 
      JOIN  ${roleMenuSqlName}
      ON ${roleMenuSqlName}.menu_id = ${menuSqlName}.id
      WHERE role = ${id}
    `;
    let list = await MYSQL.promise().query(query);

    let formattList = formatMenusList(list[0])

    return {
      code: 0,
      msg: "获取成功",
      data:formattList,  
    };
  } catch (error) {
    let {code,msg} = error
    throw { code, msg };
  }
};
